package com.zmj.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zmj.dto.SysMenuDTO;
import com.zmj.dto.SysUserDTO;
import com.zmj.dto.TableData;
import com.zmj.entity.SysUser;
import com.zmj.mapper.SysMenuMapper;
import com.zmj.mapper.SysUserMapper;
import com.zmj.utils.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    public Response login(SysUserDTO sysUserDTO) {
        SysUser sysUser = sysUserMapper.findUserByName(sysUserDTO.getUsername());


        if (sysUser == null) {
            return Response.error("账号不存在");
        }
        if (!sysUser.getPassword().equals(sysUserDTO.getPassword())) {
            return Response.error("密码错误！");
        }

        return Response.success().put("sysUser",sysUser);


    }
    public TableData findUserBypage(String search,String order,int limit,int offset){
        PageHelper.offsetPage(offset,limit);
        List<SysUser> list =sysUserMapper.findUserBypage(search,order);
        PageInfo pageInfo=new PageInfo(list);
        TableData tableData=new TableData();
        tableData.setRows(pageInfo.getList());
        tableData.setTotal(pageInfo.getTotal());
        return tableData;
    }

    public Response savaUser(SysUser sysUser){
        int i=sysUserMapper.insertSelective(sysUser);
        return  i>0?Response.success():Response.error();
    }

    public Response updateUser(SysUser sysUser){
        int i=sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return i>0?Response.success():Response.error();
    }

    public Response findUser(long userId){
        return  Response.success().put("user",sysUserMapper.selectByPrimaryKey(userId));

    }
    public  Response del(List<Long> userId){
        int i=sysUserMapper.del(userId);
        return i>0?Response.success():Response.error();
    }
    public Response findAll() {
        List<SysUser> list =   sysUserMapper.findUserBypage(null,"asc");

        return Response.success().put("userList",list);
    }

}
